<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-s-data"></i>订单管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-plus" @click="addVisible = true">添加</el-button>
                <el-button type="primary" @click="moneyVisible = true">查询订单金额</el-button>
            </div>
            <el-table
                v-loading="loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column label="序号" width="55" align="center">
                    <template slot-scope="scope">{{ scope.$index + 1 }}</template>
                </el-table-column>
                <el-table-column prop="productCode" label="产品编号" align="center"></el-table-column>
                <el-table-column prop="orderPrice" label="订单价格" align="center"></el-table-column>
                <el-table-column prop="orderType" label="订单状态" align="center"></el-table-column>
                <el-table-column prop="createBy" label="创建人" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="700px">
            <AddDialog @orderAdd="orderAdd" :addVisible="addVisible" />
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="700px">
            <EditDialog @editOrder="editOrder" :editOneData="editOneData" />
        </el-dialog>

        <!-- 查询金额 -->
        <el-dialog title="查询订单金额" :visible.sync="moneyVisible" width="700px">
            <CheckMoney />
        </el-dialog>
    </div>
</template>

<script>
import AddDialog from '@/components/OrderManage/addDialog.vue';
import EditDialog from '@/components/OrderManage/editDialog.vue';
import CheckMoney from '@/components/OrderManage/checkMoney.vue';
import { orderFindList, orderAdd, orderUpdate, orderDelete } from 'services/OrderManage';
export default {
    name: 'basetable',
    components: { AddDialog, EditDialog, CheckMoney },
    data() {
        return {
            loading: true,
            query: {
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            addVisible: false,
            editVisible: false,
            moneyVisible: false,
            editOneData: {},
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        async editOrder(params) {
            const res = await orderUpdate(params);
            if (res.code === '0') {
                this.$message({
                    message: '提交成功',
                    type: 'success'
                });
                this.editVisible = false;
                this.getData();
            } else {
                this.$message.error('操作错误');
            }
        },
        async orderAdd(params) {
            const res = await orderAdd(params);
            if (res.code === '0') {
                this.$message({
                    message: '提交成功',
                    type: 'success'
                });
                this.addVisible = false;
                this.getData();
            } else {
                this.$message.error('操作错误');
            }
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading = true;
            orderFindList(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
                this.pageTotal = res.pageTotal;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.orderDelete(row);
                })
                .catch(() => {});
        },
        async orderDelete(row) {
            const res = await orderDelete({ id: row.id });
            if (res.code === '0') {
                this.getData();
            }
        },
        // 编辑操作
        handleEdit(index, row) {
            this.editOneData = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
    margin-left: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
